<template>
  <div class="material-container">
    <!-- 搜索工作栏 -->
    <ContentWrap class="search-wrapper">
      <!-- 搜索头部 -->
      <div class="search-header">
        <div class="header-icon">
          <Icon icon="ep:picture" />
        </div>
        <div class="header-content">
          <h3 class="search-title">素材管理</h3>
          <p class="search-subtitle">管理企业微信素材库</p>
        </div>
      </div>

      <!-- 搜索表单 -->
      <div class="search-form-container">
        <el-form
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="68px"
          class="enhanced-search-form"
        >
          <div class="form-grid">
            <el-form-item label="素材名称" prop="materialName" class="form-item">
              <el-input
                v-model="queryParams.materialName"
                placeholder="请输入素材名称"
                clearable
                @keyup.enter="handleQuery"
                class="enhanced-input"
              >
                <template #prefix>
                  <Icon icon="ep:picture" />
                </template>
              </el-input>
            </el-form-item>

            <el-form-item label="创建时间" prop="createTime" class="form-item">
              <el-date-picker
                v-model="queryParams.createTime"
                value-format="YYYY-MM-DD HH:mm:ss"
                type="daterange"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
                class="enhanced-date-picker"
              />
            </el-form-item>
          </div>

          <div class="form-actions">
            <el-button @click="handleQuery" class="search-btn">
              <Icon icon="ep:search" class="btn-icon" />
              搜索
            </el-button>
            <el-button @click="resetQuery" class="reset-btn">
              <Icon icon="ep:refresh" class="btn-icon" />
              重置
            </el-button>
            <el-upload
              class="upload-component"
              :id="uuid"
              :action="uploadUrl"
              :headers="{
                Authorization: 'Bearer ' + getAccessToken(),
                'tenant-id': getTenantId()
              }"
              :before-upload="beforeUpload"
              :on-success="handleFileSuccess"
              :show-file-list="false"
              accept="image/jpeg, image/png"
            >
              <el-button
                type="primary"
                plain
                class="upload-btn"
                v-hasPermi="['wecom:material:create']"
              >
                <Icon icon="ep:plus" class="btn-icon" />
                上传素材
              </el-button>
            </el-upload>
          </div>
        </el-form>
      </div>
    </ContentWrap>

    <!-- 素材列表 -->
    <ContentWrap class="material-wrapper">
      <!-- 列表头部 -->
      <div class="list-header">
        <div class="header-icon">
          <Icon icon="ep:folder-opened" />
        </div>
        <div class="header-content">
          <h4 class="list-title">素材库</h4>
          <p class="list-subtitle">共 {{ total }} 个素材</p>
        </div>
      </div>

      <!-- 素材网格 -->
      <div class="material-grid">
        <div class="material-item" v-for="item in list" :key="item.id">
          <div class="material-card">
            <div class="image-container">
              <el-image
                :src="item.localUrl"
                fit="cover"
                :preview-src-list="[item.localUrl]"
                class="material-image"
                loading="lazy"
              >
                <template #error>
                  <div class="image-error">
                    <Icon icon="ep:picture" />
                    <span>加载失败</span>
                  </div>
                </template>
              </el-image>

              <!-- 悬浮操作 -->
              <div class="image-overlay">
                <div class="overlay-actions">
                  <el-button
                    circle
                    size="small"
                    type="danger"
                    @click="handleDelete(item.id)"
                    class="delete-btn"
                  >
                    <Icon icon="ep:delete" />
                  </el-button>
                </div>
              </div>
            </div>

            <!-- 素材信息 -->
            <div class="material-info">
              <div class="material-name">{{ item.materialName || '未命名素材' }}</div>
              <div class="material-meta">
                <Icon icon="ep:clock" class="meta-icon" />
                <span class="meta-text">{{ formatDate(item.createTime) }}</span>
              </div>
            </div>
          </div>
        </div>

        <!-- 空状态 -->
        <div v-if="list.length === 0" class="empty-state">
          <div class="empty-icon">
            <Icon icon="ep:picture" />
          </div>
          <div class="empty-text">
            <h4>暂无素材</h4>
            <p>点击上传按钮添加素材</p>
          </div>
        </div>
      </div>

      <!-- 分页 -->
      <div class="pagination-wrapper">
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </ContentWrap>
  </div>
</template>

<script setup lang="ts">
import * as MaterialApi from '@/api/wecom/material'
import { getAccessToken, getTenantId } from '@/utils/auth'
import { generateUUID } from '@/utils'
import { Delete } from '@element-plus/icons-vue'
import { formatDate } from '@/utils/formatTime'

defineOptions({ name: 'Material' })

const uuid = ref('id-' + generateUUID())
const uploadHeaders = ref()

// 上传地址
const uploadUrl = import.meta.env.VITE_BASE_URL + '/admin-api/wecom/material/create'

// 国际化
const { t } = useI18n()

// 列表的总页数
const total = ref(0)
// 列表的数据
const list = ref([])
const queryParams = reactive({
  pageNo: 1,
  pageSize: 18,
  materialName: null,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  try {
    const data = await MaterialApi.getMaterialPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/**上传之前*/
const beforeUpload = (file: File) => {
  const isImage = file.type === 'image/jpeg' || file.type === 'image/png'
  const isSizeValid = file.size > 5 // 大于5个字节
  const isSizeLimit = file.size <= 10 * 1024 * 1024 // 小于等于10MB

  if (!isImage) {
    ElMessage.error('上传图片只能是 JPG 或 PNG 格式!')
  }
  if (!isSizeValid) {
    ElMessage.error('文件大小必须大于 5 个字节!')
  }
  if (!isSizeLimit) {
    ElMessage.error('上传图片大小不能超过 10MB!')
  }

  return isImage && isSizeValid && isSizeLimit
}
/**上传成功处理*/
const handleFileSuccess = (res) => {
  if (res.code == 0) {
    ElMessage.success('上传成功!')
    handleQuery()
  } else {
    ElMessage.error(res.msg)
  }
}
/**删除*/
const handleDelete = (id) => {
  ElMessageBox.confirm('您确定要删除此素材?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    const res = await MaterialApi.deleteMaterial(id)
    if (res) {
      ElMessage.success('删除成功!')
      handleQuery()
    }
  })
}
/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style scoped lang="scss">
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

.material-container {
  padding: 16px;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: 100vh;

  @media (max-width: 768px) {
    padding: 8px;
  }
}

/* 搜索区域样式 */
.search-wrapper {
  margin-bottom: 16px;
  border-radius: $border-radius;
  overflow: hidden;
  box-shadow: $shadow-light;
  border: 1px solid $border-light;

  :deep(.el-card__body) {
    padding: 0;
  }
}

.search-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;
  border-bottom: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px;
    flex-direction: column;
    gap: 12px;
    text-align: center;
  }
}

.header-icon {
  width: 48px;
  height: 48px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }
}

.header-content {
  flex: 1;
  margin-left: 16px;

  @media (max-width: 768px) {
    margin-left: 0;
  }
}

.search-title {
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 18px;
  }
}

.search-subtitle {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

.search-form-container {
  background: white;
  padding: 24px;

  @media (max-width: 768px) {
    padding: 16px;
  }
}

.enhanced-search-form {
  .form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 20px;
    margin-bottom: 20px;

    @media (max-width: 768px) {
      grid-template-columns: 1fr;
      gap: 16px;
    }
  }

  .form-item {
    margin-bottom: 0;

    :deep(.el-form-item__label) {
      font-weight: 500;
      color: $text-regular;
    }
  }
}

.enhanced-input,
.enhanced-date-picker {
  width: 100%;

  :deep(.el-input__wrapper) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
    }
  }
}

.form-actions {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;

  @media (max-width: 768px) {
    flex-direction: column;
    align-items: stretch;
  }
}

.search-btn {
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  border: none;
  border-radius: $border-radius-small;
  padding: 10px 20px;
  font-weight: 500;
  color: white;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
  }

  @media (max-width: 768px) {
    width: 100%;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

.reset-btn {
  border: 2px solid $border-color;
  color: $text-regular;
  border-radius: $border-radius-small;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.3s ease;

  &:hover {
    border-color: $primary-color;
    color: $primary-color;
    background: $primary-light;
  }

  @media (max-width: 768px) {
    width: 100%;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

.upload-component {
  @media (max-width: 768px) {
    width: 100%;
  }
}

.upload-btn {
  border: 2px solid $primary-color;
  color: $primary-color;
  border-radius: $border-radius-small;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.3s ease;

  &:hover {
    background: $primary-light;
    transform: translateY(-1px);
  }

  @media (max-width: 768px) {
    width: 100%;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

/* 素材列表区域 */
.material-wrapper {
  border-radius: $border-radius;
  overflow: hidden;
  box-shadow: $shadow-light;
  border: 1px solid $border-light;

  :deep(.el-card__body) {
    padding: 0;
  }
}

.list-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%);
  border-bottom: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px;
  }

  .header-icon {
    width: 40px;
    height: 40px;
    background: $primary-color;
    border-radius: $border-radius-small;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 18px;
    flex-shrink: 0;

    @media (max-width: 768px) {
      width: 36px;
      height: 36px;
      font-size: 16px;
    }
  }

  .header-content {
    flex: 1;
    margin-left: 12px;

    @media (max-width: 768px) {
      margin-left: 10px;
    }
  }

  .list-title {
    font-size: 16px;
    font-weight: 600;
    color: $text-primary;
    margin: 0 0 2px 0;

    @media (max-width: 768px) {
      font-size: 14px;
    }
  }

  .list-subtitle {
    font-size: 13px;
    color: $text-secondary;
    margin: 0;

    @media (max-width: 768px) {
      font-size: 12px;
    }
  }
}

/* 素材网格 */
.material-grid {
  padding: 24px;
  background: white;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 20px;
  min-height: 280px;

  @media (max-width: 768px) {
    padding: 16px;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 16px;
  }

  @media (max-width: 480px) {
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 12px;
  }
}

.material-item {
  display: flex;
  flex-direction: column;
}

.material-card {
  background: white;
  border-radius: $border-radius-small;
  overflow: hidden;
  box-shadow: $shadow-base;
  border: 1px solid $border-light;
  transition: all 0.3s ease;
  height: 100%;
  display: flex;
  flex-direction: column;

  &:hover {
    transform: translateY(-4px);
    box-shadow: $shadow-light;

    .image-overlay {
      opacity: 1;
    }
  }
}

.image-container {
  position: relative;
  width: 100%;
  height: 200px;
  overflow: hidden;
  flex-shrink: 0;

  @media (max-width: 768px) {
    height: 160px;
  }

  @media (max-width: 480px) {
    height: 140px;
  }
}

.material-image {
  width: 100%;
  height: 100%;
  transition: all 0.3s ease;

  &:hover {
    transform: scale(1.05);
  }

  :deep(.el-image__inner) {
    transition: all 0.3s ease;
  }
}

.image-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  background: #f5f7fa;
  color: $text-placeholder;
  font-size: 14px;
  gap: 8px;

  svg {
    font-size: 32px;
  }
}

.image-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: all 0.3s ease;
}

.overlay-actions {
  display: flex;
  gap: 8px;
}

.delete-btn {
  background: rgba(245, 108, 108, 0.9);
  border: none;
  color: white;
  width: 40px;
  height: 40px;
  transition: all 0.3s ease;

  &:hover {
    background: $danger-color;
    transform: scale(1.1);
  }

  @media (max-width: 768px) {
    width: 36px;
    height: 36px;
  }
}

.material-info {
  padding: 12px;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;

  @media (max-width: 768px) {
    padding: 10px;
  }
}

.material-name {
  font-size: 14px;
  font-weight: 500;
  color: $text-primary;
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;

  @media (max-width: 768px) {
    font-size: 13px;
  }
}

.material-meta {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: $text-secondary;

  @media (max-width: 768px) {
    font-size: 11px;
  }
}

.meta-icon {
  font-size: 12px;
  flex-shrink: 0;
}

.meta-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 空状态 */
.empty-state {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  text-align: center;

  @media (max-width: 768px) {
    padding: 40px 20px;
  }
}

.empty-icon {
  width: 80px;
  height: 80px;
  background: $primary-light;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
  color: $primary-color;
  margin-bottom: 16px;

  @media (max-width: 768px) {
    width: 64px;
    height: 64px;
    font-size: 24px;
  }
}

.empty-text {
  h4 {
    font-size: 18px;
    font-weight: 600;
    color: $text-primary;
    margin: 0 0 8px 0;

    @media (max-width: 768px) {
      font-size: 16px;
    }
  }

  p {
    font-size: 14px;
    color: $text-secondary;
    margin: 0;

    @media (max-width: 768px) {
      font-size: 12px;
    }
  }
}

/* 分页样式 */
.pagination-wrapper {
  display: flex;
  justify-content: center;
  padding: 20px 24px;
  background: white;
  border-top: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px;
  }

  :deep(.el-pagination) {
    .el-pager li {
      border-radius: 6px;
      margin: 0 2px;
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }

      &.is-active {
        background: $primary-color;
        color: white;
      }
    }

    .btn-prev,
    .btn-next {
      border-radius: 6px;
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }
    }

    @media (max-width: 768px) {
      .el-pagination__sizes,
      .el-pagination__jump {
        display: none;
      }
    }
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .material-container {
    padding: 4px;
  }

  .search-form-container {
    padding: 12px;
  }

  .material-grid {
    padding: 12px;
  }

  .material-info {
    padding: 8px;
  }

  .pagination-wrapper {
    padding: 12px;
  }
}

/* 加载状态优化 */
:deep(.el-loading-mask) {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);

  .el-loading-spinner {
    .path {
      stroke: $primary-color;
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .material-container {
    background: linear-gradient(135deg, #1f2937 0%, #111827 100%);
  }

  .search-wrapper,
  .material-wrapper {
    background: #374151;
    border-color: #4b5563;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  }

  .search-form-container,
  .material-grid,
  .pagination-wrapper {
    background: #374151;
  }

  .list-header {
    background: #4b5563;
    border-color: #6b7280;
  }

  .search-title,
  .list-title {
    color: #f9fafb;
  }

  .material-card {
    background: #374151;
    border-color: #4b5563;
  }

  .material-name {
    color: #f9fafb;
  }

  .material-meta {
    color: #d1d5db;
  }

  .image-error {
    background: #4b5563;
    color: #9ca3af;
  }

  .empty-text {
    h4 {
      color: #f9fafb;
    }

    p {
      color: #d1d5db;
    }
  }
}
</style>
